﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProjControleBLL;

namespace ProjControle
{
    public partial class frm_consulta : Form
    {
        public int codigo;
        public String controle;
        public frm_consulta()
        {
            InitializeComponent();
        }
        #region Métodos
        public void associaGrid()
        {
            switch (controle)
            {
                case "C":
                                       
                    try
                    {
                        DataTable tab = new DataTable();
                       ClienteBLL objBLL = new ClienteBLL();
                        tab.Columns.Add("cod_cliente");
                        tab.Columns.Add("nome_cliente");
                        tab.Columns.Add("status_cliente");
                        tab.Columns.Add("estado_cliente");
                        tab.Columns.Add("num_casa");
                        dgConsulta.DataSource = tab;
                        dgConsulta.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                        dgConsulta.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                        dgConsulta.AllowUserToAddRows = false;
                        dgConsulta.AllowUserToResizeRows = false;
                        dgConsulta.AllowUserToResizeColumns = false;
                        dgConsulta.Columns["cod_cliente"].HeaderText = "Código";
                        dgConsulta.Columns["cod_cliente"].Width = 70;
                        dgConsulta.Columns["nome_cliente"].HeaderText = "Nome";
                        dgConsulta.Columns["nome_cliente"].Width = 300;
                        dgConsulta.Columns["status_cliente"].HeaderText = "Bloqueado?";
                        dgConsulta.Columns["status_cliente"].Width = 100;
                        dgConsulta.Columns["num_casa"].Visible = false;
                        dgConsulta.Columns["estado_cliente"].Visible = false;
                        //objBLL.localizar("");
                        objBLL = null;
                    }
                    catch (Exception ex)
                    {

                        MessageBox.Show(ex.Message);
                    }
                    
                 break;
                case "F":
                 {
                     try
                     {
                         DataTable tab = new DataTable();
                         FuncionarioBLL objBLL = new FuncionarioBLL();
                         tab.Columns.Add("cod_funcionario");
                         tab.Columns.Add("nome_funcionario");
                         dgConsulta.DataSource = tab;
                         dgConsulta.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                         dgConsulta.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                         dgConsulta.AllowUserToAddRows = false;
                         dgConsulta.AllowUserToResizeRows = false;
                         dgConsulta.AllowUserToResizeColumns = false;
                         dgConsulta.Columns["cod_funcionario"].Width = 100;
                         dgConsulta.Columns["cod_funcionario"].HeaderText = "Código";
                         dgConsulta.Columns["nome_funcionario"].Width = 300;
                         dgConsulta.Columns["nome_funcionario"].HeaderText = "Funcionario";
                         dgConsulta.DataSource = objBLL.localizar("");
                         for (int i = 2; i < dgConsulta.Columns.Count; i++)
                             dgConsulta.Columns[i].Visible = false;
                     }
                     catch (Exception ex)
                     {

                         MessageBox.Show(ex.Message);
                     }
                     break;
                 }
                case "P":
                 {
                     try
                     {
                         DataTable tab = new DataTable();
                         ProdutosBLL objBLL = new ProdutosBLL();
                         tab.Columns.Add("cod_produto");
                         tab.Columns.Add("descricao_produto");
                         tab.Columns.Add("qtd_prod");
                         tab.Columns.Add("preco_venda");
                         dgConsulta.DataSource = tab;
                         dgConsulta.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                         dgConsulta.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                         dgConsulta.AllowUserToAddRows = false;
                         dgConsulta.AllowUserToResizeRows = false;
                         dgConsulta.AllowUserToResizeColumns = false;
                         dgConsulta.Columns["cod_produto"].Width = 100;
                         dgConsulta.Columns["cod_produto"].HeaderText = "Código";
                         dgConsulta.Columns["descricao_produto"].Width = 250;
                         dgConsulta.Columns["descricao_produto"].HeaderText = "Descrição";
                         dgConsulta.Columns["qtd_prod"].Width = 100;
                         dgConsulta.Columns["qtd_prod"].HeaderText = "Quantidade";
                         dgConsulta.Columns["preco_venda"].Width = 100;
                         dgConsulta.Columns["preco_venda"].HeaderText = "Preço";
                         dgConsulta.DataSource = objBLL.localizar("");
                         for (int i = 4; i < dgConsulta.Columns.Count; i++)
                             dgConsulta.Columns[i].Visible = false;
                        

                     }
                     catch (Exception ex)
                     {

                         MessageBox.Show(ex.Message);
                     }
                 }
                 break;
                case "S":
                 {
                     try
                     {
                         DataTable tab = new DataTable();
                         ServicosBLL objBLL = new ServicosBLL();
                         tab.Columns.Add("cod_servico");
                         tab.Columns.Add("descricao_servico");
                         tab.Columns.Add("val_servico");
                         dgConsulta.DataSource = tab;
                         dgConsulta.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                         dgConsulta.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                         dgConsulta.AllowUserToAddRows = false;
                         dgConsulta.AllowUserToResizeRows = false;
                         dgConsulta.AllowUserToResizeColumns = false;
                         dgConsulta.Columns["cod_servico"].Width = 100;
                         dgConsulta.Columns["cod_servico"].HeaderText = "Código";
                         dgConsulta.Columns["descricao_servico"].Width = 250;
                         dgConsulta.Columns["descricao_servico"].HeaderText = "Descrição";
                         dgConsulta.Columns["val_servico"].Width = 100;
                         dgConsulta.Columns["val_servico"].HeaderText = "Preço";
                         dgConsulta.DataSource = objBLL.localizar();
                         for (int i = 4; i < dgConsulta.Columns.Count; i++)
                             dgConsulta.Columns[i].Visible = false;
                     }
                     catch (Exception ex)
                     {

                         MessageBox.Show(ex.Message);
                     }
                     break;
                 }
                case "V":
                 {
                     try
                     {
                         DataTable tab = new DataTable();
                         VendaBLL objBLL = new VendaBLL();
                         tab.Columns.Add("cod_venda");
                         tab.Columns.Add("nome_cliente");
                         tab.Columns.Add("val_venda");
                         tab.Columns.Add("dt_venda");
                         dgConsulta.DataSource = tab;
                         dgConsulta.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                         dgConsulta.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                         dgConsulta.AllowUserToAddRows = false;
                         dgConsulta.AllowUserToResizeRows = false;
                         dgConsulta.AllowUserToResizeColumns = false;
                         dgConsulta.Columns["cod_venda"].Width = 100;
                         dgConsulta.Columns["cod_venda"].HeaderText = "Código";
                         dgConsulta.Columns["nome_cliente"].Width = 250;
                         dgConsulta.Columns["nome_cliente"].HeaderText = "Cliente";
                         dgConsulta.Columns["val_venda"].Width = 100;
                         dgConsulta.Columns["val_venda"].HeaderText = "Valor Venda";
                         dgConsulta.Columns["dt_venda"].Width = 100;
                         dgConsulta.Columns["dt_venda"].HeaderText = "Data";
                         dgConsulta.DataSource = objBLL.localizar(tbLocalizar.Text);
                         for (int i = 4; i < dgConsulta.Columns.Count; i++)
                             dgConsulta.Columns[i].Visible = false;
                     }
                     catch (Exception ex)
                     {

                         MessageBox.Show(ex.Message);
                     }
                 }
                 break;
                case "FO":
                 {
                     try
                     {
                         DataTable tab = new DataTable();
                         FornecedoresBLL objBLL = new FornecedoresBLL();
                         tab.Columns.Add("cod_fornecedor");
                         tab.Columns.Add("descricao_fornecedor");
                         dgConsulta.DataSource = tab;
                         dgConsulta.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                         dgConsulta.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                         dgConsulta.AllowUserToAddRows = false;
                         dgConsulta.AllowUserToResizeRows = false;
                         dgConsulta.AllowUserToResizeColumns = false;
                         dgConsulta.Columns["cod_fornecedor"].Width = 100;
                         dgConsulta.Columns["cod_fornecedor"].HeaderText = "Código";
                         dgConsulta.Columns["descricao_fornecedor"].Width = 250;
                         dgConsulta.Columns["descricao_fornecedor"].HeaderText = "Fornecedor";
                         dgConsulta.DataSource = objBLL.localizar(tbLocalizar.Text);
                         for (int i = 2; i < dgConsulta.Columns.Count; i++)
                             dgConsulta.Columns[i].Visible = false;
                     }
                     catch (Exception ex)
                     {

                         MessageBox.Show(ex.Message);
                     }
                 }
                 break;
            }
                   
        }
        #endregion

        private void frm_consulta_Load(object sender, EventArgs e)
        {
            associaGrid();
        }

        private void tbLocalizar_KeyPress(object sender, KeyPressEventArgs e)
        {
           switch (controle)
           {
               case "C":
                   {
                       ClienteBLL objBLL;
                       if (e.KeyChar == Convert.ToChar(Keys.Space))
                       {
                           try
                           {
                               DataTable tab;
                               objBLL = new ClienteBLL();
                               tab = objBLL.localizar(tbLocalizar.Text.Trim());
                               if (tab.Rows.Count > 0)
                               {
                                   dgConsulta.DataSource = tab;
                                 for (int i = 2; i < 11; i++)
                                      dgConsulta.Columns[i].Visible = false;
                                 for (int i = 14; i < dgConsulta.Columns.Count; i++)
                                     dgConsulta.Columns[i].Visible = false;
                               }
                               else
                                   MessageBox.Show("Registro(s) não Encontrado(s)!", "Clientes", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                           }
                           catch (Exception ex)
                           {

                               MessageBox.Show(ex.Message);
                           }
                       }
                   }
                   break;
               case "V":
                   {
                       VendaBLL objBLL;
                       if (e.KeyChar == Convert.ToChar(Keys.Space))
                       {
                           try
                           {
                               DataTable tab;
                               objBLL = new VendaBLL();
                               tab = objBLL.localizar(tbLocalizar.Text.Trim());
                               if (tab.Rows.Count > 0)
                                    dgConsulta.DataSource = tab;
                               else
                                   MessageBox.Show("Registro(s) não Encontrado(s)!", "Clientes", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                           }
                           catch (Exception ex)
                           {

                               MessageBox.Show(ex.Message);
                           }
                       }
                   }
                   break;
               case "P":
                   {
                       ProdutosBLL objBLL;
                       if (e.KeyChar == Convert.ToChar(Keys.Space))
                       {
                           try
                           {
                               DataTable tab;
                               objBLL = new ProdutosBLL();
                               tab = objBLL.localizar(tbLocalizar.Text.Trim());
                               if (tab.Rows.Count > 0)
                                   dgConsulta.DataSource = tab;
                               else
                                   MessageBox.Show("Registro(s) não Encontrado(s)!", "Clientes", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                           }
                           catch (Exception ex)
                           {

                               MessageBox.Show(ex.Message);
                           }
                       }
                   }
                   break;
           }
              
        }

        private void dgConsulta_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                codigo = int.Parse(dgConsulta.Rows[e.RowIndex].Cells[0].Value.ToString());
                this.Close();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private void frm_consulta_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == Convert.ToChar(Keys.Escape))
                this.Close();
        }
    }
}
